package com.mingqijia.gassafety.webserver.service.impl;

import java.util.*;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gap.basic.exception.BizException;
import com.gap.cache.redis.RedisCache;
import com.github.pagehelper.util.StringUtil;
import com.mingqijia.gassafety.authority.api.resp.config.DictionaryItemRespDTO;
import com.mingqijia.gassafety.authority.dal.helper.ConfigureHolder;
import com.mingqijia.gassafety.db.entity.*;
import com.mingqijia.gassafety.db.entity.redis.*;
import com.mingqijia.gassafety.db.mapper.DeviceStateMapper;
import com.mingqijia.gassafety.db.mapper.WarningAlarmMapper;
import com.mingqijia.gassafety.shared.constant.*;
import com.mingqijia.gassafety.shared.utils.ApplicationUtils;
import com.mingqijia.gassafety.shared.utils.RedisCacheExt;
import com.mingqijia.gassafety.webserver.request.BrowserNotifyItemRequest;
import com.mingqijia.gassafety.webserver.request.BrowserNotifyReason;
import com.mingqijia.gassafety.webserver.response.BrowserNotifyResponse;
import com.mingqijia.gassafety.webserver.response.OrganizationResponse;
import com.mingqijia.gassafety.webserver.response.SmallBellResponse;
import com.mingqijia.gassafety.webserver.service.*;
import com.mingqijia.gassafety.webserver.service.workorder.DtuBindingOrderService;
import com.mingqijia.gassafety.webserver.webSocket.WebSocketProcess;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.stream.Collectors;

import static com.mingqijia.gassafety.shared.constant.Constants.*;

/**
 * <p>
 * 本地消息 服务实现类
 * </p>
 *
 * @author guoliang.lao
 * @since 2022-03-04
 */
@Service
@Slf4j
public class BrowserNotifyServiceImpl implements BrowserNotifyService {
    @Autowired
    FieldTemplateService fieldTemplateService;
    @Autowired
    WarningRecordService warningRecordService;
    @Autowired
    EquipmentService equipmentService;
    @Autowired
    WebSocketProcess webSocketProcess;
    @Autowired
    BusinessNoticeService businessNoticeService;
    @Autowired
    ConfigureHolder holder;
    @Autowired
    ConsumerService consumerService;
    @Autowired
    RedisCache redisCache;
    @Autowired
    RedisCacheExt redisCacheExt;
    @Autowired
    LatestLogService latestLogService;
    @Autowired
    DtuBindingOrderService dtuBindingOrderService;
    @Autowired
    private OrganizationService organizationService;
    @Autowired
    DeviceStateMapper deviceStateMapper;

    @Autowired
    WarningAlarmMapper warningAlarmMapper;

    @Autowired
    ServicePkgRecordService servicePkgRecordService;

    public static final String SYSTEM_ALERT_NOTICE_SETTINGS = "system_alert_notice_settings";

    @Override
    @Async
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void notify(List<BrowserNotifyItemRequest> items) {
        try {
            for (BrowserNotifyItemRequest item : items) {
                log.info(item.toString());
                BrowserNotifyResponse notify = new BrowserNotifyResponse();
                notify.setNotifyReason(item.getReason());
                notify.setNotifyTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                notify.setXlel(String.valueOf(item.getXlel()));
                if (!StringUtils.isEmpty(item.getValveStatus()))
                    notify.setValveStatus(item.getValveStatus());
                if (!StringUtils.isEmpty(item.getReceiverStatus()))
                    notify.setReceiverStatus(item.getReceiverStatus());
                //查询设备信息
                String imei = "";
//                Double dtuType = Double.valueOf(item.getDtuType());
                if (EquipmentType.POINT_TYPE_GAS_DETECTOR.getCode().equals(item.getEquipmentType())) {
                    if (StringUtils.isEmpty(item.getSubIdentifier()) || item.getSubIdentifier().equals("null")) {
                        imei = item.getDtuId();
                    } else {
                        imei = item.getDtuId() + "-" + item.getSubIdentifier();
                    }
                } else {
                    imei = item.getDtuId();
                }

                LambdaQueryWrapper<Dtu> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(Dtu::getIMEI, imei).eq(Dtu::getIsDeleted, 0);
                Dtu dtu = equipmentService.getOne(queryWrapper);
                if (dtu == null) {
                    log.info("设备不存在:" + imei);
                    return;
                }
                boolean servicePkgModeSwitch = false;
                List<String> servicePkgConfigs = new ArrayList<>();
                if (dtu.getConsumerId() != null && dtu.getConsumerId() > 0) {
                    Consumer consumer = consumerService.getById(dtu.getConsumerId());

                    servicePkgModeSwitch = businessNoticeService.switchValueByCode(
                            BusinessNoticeParam.DIFFERENT_SERVICE_PACKAGE_MODE.getCode(),
                            consumer.getSpId()
                    );
                    servicePkgConfigs = servicePkgRecordService.servicePkgSendConfig(consumer, dtu.getIMEI(),servicePkgModeSwitch);
                    boolean isAddSmallBell = !servicePkgModeSwitch || servicePkgConfigs.contains(ServicePkgEnum.SYSTEM_SMALL_TRUMPET_ALARM.getCode());
                    log.info("isAddSmallBell:{}",isAddSmallBell);
                    notify.setConsumerId(consumer.getId());
                    //如果是紫荆设备
                    if (dtu.getEquipmentFrom() == Constants.ZJ_EQUIPMENT_FROM) {
                        notify.setMemberId(consumer.getMemberId());
                        notify.setPhone(consumer.getPhone());
                        updateSmallBell(dtu, notify.getNotifyTime(), consumer, null, isAddSmallBell);
                    } else {
                        QueryWrapper<FieldTemplate> templateQueryWrapper = new QueryWrapper<>();
                        templateQueryWrapper.lambda().eq(FieldTemplate::getSpId, dtu.getSpId())
                                .eq(FieldTemplate::getIsDeleted, 0);
                        FieldTemplate fieldTemplate = fieldTemplateService.getOne(templateQueryWrapper);
                        if (BIND_CHANNEL_GAS == fieldTemplate.getTemplateType()) {
                            log.info("消息推送为模板一");
                            notify.setCustName(StringUtils.isEmpty(consumer.getCustName()) ? "" : consumer.getCustName());
                            notify.setSubsCode(StringUtils.isEmpty(consumer.getSubsCode()) ? "" : consumer.getSubsCode());
                            //新增或删除报警小铃铛
                            updateSmallBell(dtu, notify.getNotifyTime(), consumer, null, isAddSmallBell);
                        } else if (BIND_CHANNEL_ASST == fieldTemplate.getTemplateType()) {
                            notify.setMemberId(consumer.getMemberId());
                            if (dtu.getEquipmentFrom() == Constants.ZJ_EQUIPMENT_FROM) {
                                notify.setCustName(StringUtils.isEmpty(consumer.getCustName()) ? "" : consumer.getCustName());
                                notify.setSubsCode(StringUtils.isEmpty(consumer.getSubsCode()) ? "" : consumer.getSubsCode());
                                //新增或删除报警小铃铛
                                updateSmallBell(dtu, notify.getNotifyTime(), consumer, null, isAddSmallBell);
                            } else {
                                QueryWrapper<DtuBindingOrder> wrapper = new QueryWrapper<>();
                                wrapper.lambda().eq(DtuBindingOrder::getDeviceId, imei)
                                        .eq(DtuBindingOrder::getConsumerId, consumer.getId())
                                        .eq(DtuBindingOrder::getIsDeleted, false);
                                DtuBindingOrder order = dtuBindingOrderService.getOne(wrapper);
                                if (order == null) {
                                    log.info("设备:{} 未查询到订单信息", dtu.getId());
                                    throw new BizException("未查询到订单信息");
                                }
                                notify.setOrderContactName(order.getCustName());
                                //新增或删除报警小铃铛
                                updateSmallBell(dtu, notify.getNotifyTime(), consumer, order.getCustName(), isAddSmallBell);
                            }
                        }
                    }
                }

                //查询通知规则
                Map<String, Object> map = businessNoticeService.isSend(item.getReason(), dtu.getSpId());

                // 直接修改故障通知的名称，修改为故障原因
                LambdaQueryWrapper<DeviceState> deviceStateQueryWrapper = new LambdaQueryWrapper<>();
                deviceStateQueryWrapper
                        .eq(DeviceState::getDeviceId, dtu.getId())
                        .eq(DeviceState::getIsDeleted, 0)
                        .eq(DeviceState::getSpId, dtu.getSpId());
                DeviceState deviceState = deviceStateMapper.selectOne(deviceStateQueryWrapper);
                if ("设备故障".equals(String.valueOf(map.get("notifyName")))) {
                    notify.setNotifyName(deviceState.getBrokenStatusInfo());
                } else {
                    notify.setNotifyName(String.valueOf(map.get("notifyName")));
                }
                notify.setImei(imei);
                notify.setInstallationPosition(dtu.getInstallationPosition());
                //设备类型
                if (!StringUtils.isEmpty(dtu.getEquipmentType())) {
                    DictionaryItemRespDTO equipmentTypeDict = holder.getDictionaryByKey(
                            EquipmentType.DICT_CODE).stream().filter(a -> a.getValue().equals(
                            dtu.getEquipmentType())).collect(Collectors.toList()).get(0);
                    notify.setEquipmentTypeName(equipmentTypeDict.getName());
                }

                //系统通知
                if (Boolean.TRUE.equals(map.get("system"))) {
                    notify.setIsSystem(true);
                }
                //页面弹窗提醒
                if (Boolean.TRUE.equals(map.get("pop"))) {
                    notify.setIsPop(true);
                }

                if ( servicePkgModeSwitch ){
                    notify.setIsPop(notify.getIsPop() && servicePkgConfigs.contains(ServicePkgEnum.SYSTEM_POP_ALARM.getCode()));
                    notify.setIsSystem(notify.getIsSystem() && servicePkgConfigs.contains(ServicePkgEnum.SYSTEM_POP_ALARM.getCode()));
                }

                notify.setEquipmentFrom(item.getEquipmentFrom());
                //插入预警记录
                WarningRecord warningRecord = initWarningRecord(notify, item, dtu.getSpId());

                log.info("预警记录:" + warningRecord.toString());
                if ("报警恢复".equals(notify.getNotifyName())) {
                    // 当同一设备报警策略周期结束时，删除对应的缓存
                    redisCacheExt.del(Constants.ALARM_STRATEGY + notify.getImei() + dtu.getSpId());
                }
                if (warningRecordService.save(warningRecord)) {
                    //发送本地消息
                    webSocketProcess.redisTopicSendMsg(dtu.getSpId(), JSONObject.toJSONString(notify));
                }
            }
        } catch (Exception e) {
            log.error("本地消息通知失败", e);
        }
    }

    /**
     * job服务触发
     *
     * @param object
     */
    @Override
    public void jobNotify(JSONObject object) {
        try {

            JSONObject message = object.getJSONObject("message");
            Object notifyName = message.get("notifyName");
            Object imei = message.get("imei");
            // 使用缓存来记录一个策略推送周期推送了几条消息
            Map<String, Integer> newMap = new HashMap();
            newMap.put("policyNumber", 1);
            log.info("缓存内容：{}", redisCache.getCacheMap(Constants.ALARM_STRATEGY+ imei + object.getString("spId")).toString());
            if ("设备触发报警策略".equals(notifyName.toString())) {
                if (redisCache.getCacheMap(Constants.ALARM_STRATEGY + imei + object.getString("spId")).isEmpty()) {
                    redisCache.setCacheMap(Constants.ALARM_STRATEGY+ imei + object.getString("spId"), newMap);
                    message.put("policyNumber", newMap.get("policyNumber"));
                } else {
                    Map<String, Integer> lastMap = redisCache.getCacheMap(Constants.ALARM_STRATEGY + imei + object.getString("spId"));
                    if( lastMap.containsKey("policyNumber") ){
                        newMap.put("policyNumber",lastMap.get("policyNumber"));
                    }
                    newMap.replace("policyNumber", newMap.get("policyNumber") + 1);
                    message.put("policyNumber", newMap.get("policyNumber"));
                    redisCache.setCacheMap(Constants.ALARM_STRATEGY + imei + object.getString("spId"), newMap);
                }
            }
            log.info("notifyName:{}", notifyName.toString());

            boolean isPop = message.getBoolean("isPop");
            boolean isSystem = message.getBoolean("isSystem");
            int consumerId = null==message.get("consumerId")?0:message.getInteger("consumerId");
            int pkgId = null==message.get("pkgId")?0:message.getInteger("pkgId");
            boolean systemPopAlarm = servicePkgRecordService.checkOwnerServiceContentCode(
                    object.getString("spId"),
                    consumerId,
                    pkgId,
                    imei.toString(),
                    ServicePkgEnum.SYSTEM_POP_ALARM.getCode()
            );
            message.put("isPop", isPop && systemPopAlarm);
            message.put("isSystem", isSystem && systemPopAlarm);

            object.put("message", message);
            log.info("message:{}", object.getJSONObject("message"));
            webSocketProcess.redisTopicSendMsg(object.getString("spId"),
                    JSONObject.toJSONString(object.getJSONObject("message")));
        } catch (Exception e) {
            log.error("jobNotify-error:{}", e);
        }
    }

    @Override
    public SmallBellResponse smallBellList() {
        SmallBellResponse res = new SmallBellResponse();
        try {
            List<SmallBell> list1 = new ArrayList<>();
            List<SmallBell> list2 = new ArrayList<>();
            List<String> imeiList = new ArrayList<>();

            String spId = ApplicationUtils.getWorkingSpId();
            Map<String, String> map = redisCache.getCacheMap("alert_small_bell:" + spId);
            log.info("smallBellList-map:{},{}", spId, JSONObject.toJSONString(map));
            for (String key : map.keySet()) {
                String json = map.get(key);
                ObjectMapper mapper = new ObjectMapper();
                SmallBell smallBell = mapper.readValue(json, SmallBell.class);
                list1.add(smallBell);
                list2.add(smallBell);
                imeiList.add(smallBell.getImei());
            }

            if (!list1.isEmpty()) {
                //查询发送消息配置
                Map<String,Object> qMap = new HashMap<>();
                qMap.put("spId", spId);
                qMap.put("paramCode", SYSTEM_ALERT_NOTICE_SETTINGS);
                List<BusinessNotice> list = businessNoticeService.selectParamList(qMap);
                String systemAlertNotice = "OFF";
                if (!CollectionUtils.isEmpty(list) && null != list.get(0) && "ON".equals(list.get(0).getParamValue())) {
                    systemAlertNotice = "ON";
                }

                Map<String, List<WarningAlarm>> alarmMap = new HashMap<>();
                if ("ON".equals(systemAlertNotice)) {
                    //查询未处理的报警信息
                    LambdaQueryWrapper<WarningAlarm> warningAlarmWrapper = new LambdaQueryWrapper<>();
                    warningAlarmWrapper.eq(WarningAlarm::getIsDeleted, 0).eq(WarningAlarm::getDisposeStatus, 0)
                            .eq(WarningAlarm::getSpId, spId).in(WarningAlarm::getIMEI, imeiList);
                    List<WarningAlarm> alarmList = warningAlarmMapper.selectList(warningAlarmWrapper);
                    if (!CollectionUtils.isEmpty(alarmList)) {
                        alarmMap = alarmList.stream().collect(Collectors.groupingBy(WarningAlarm::getSpId));
                    }
                }

                //查询小铃铛对应的设备列表
                LambdaQueryWrapper<Dtu> dtuLambdaQueryWrapper = new LambdaQueryWrapper<>();
                dtuLambdaQueryWrapper.eq(Dtu::getIsDeleted, 0).in(Dtu::getIMEI, imeiList);
                List<Dtu> equipmentList = equipmentService.list(dtuLambdaQueryWrapper);
                for (SmallBell smallBell : list1) {
                    //小铃铛记录超过一天，则认为过期，删除
                    DateTime start = DateUtil.parse(smallBell.getNotifyTime());
                    DateTime end = DateUtil.date();
                    if (end.getTime() - start.getTime() > 86400000) {
                        redisCacheExt.hashDel("alert_small_bell:" + spId, smallBell.getImei());
                        list2.remove(smallBell);
                    }
                    List<Dtu> equipmentList2 = equipmentList.stream().filter(a -> a.getIMEI().equals(smallBell.getImei())).collect(Collectors.toList());
                    if (equipmentList2.isEmpty()) {
                        redisCacheExt.hashDel("alert_small_bell:" + spId, smallBell.getImei());
                        log.info("设备" + smallBell.getImei() + "不存在或已删除，小铃铛删除");
                        continue;
                    }
                    Dtu equipment = equipmentList2.get(0);
                    //根据当前状态比较
                    LatestLog latestLog = latestLogService.get(smallBell.getImei(), spId);
                    if (latestLog == null || latestLog.getEquipmentStatus() == null) {
                        //可能是其下级组织设备
                        List<Organization> childList = organizationService.getChildList(spId);
                        log.info("smallBellList-childList:{}", JSONObject.toJSONString(childList));
                        for (Organization organization : childList) {
                            LatestLog latestLogSon = latestLogService.get(smallBell.getImei(), organization.getSpId());
                            log.info("smallBellList-latestLog:{}", JSONObject.toJSONString(latestLogSon));
                            if (latestLogSon != null && latestLogSon.getEquipmentStatus() != null) {
                                latestLog = latestLogSon;
                                continue;
                            }
                        }
                    }
                    log.info("smallBellList-latestLog:{}", JSONObject.toJSONString(latestLog));
                    if (latestLog == null || latestLog.getEquipmentStatus() == null) {
                        //状态非报警，删除小铃铛
                        redisCacheExt.hashDel("alert_small_bell:" + spId, smallBell.getImei());
                        list2.remove(smallBell);
                    } else {
                        if (latestLog.getEquipmentStatus().equals(EquipmentStatus.lowAlert) ||
                                latestLog.getEquipmentStatus().equals(EquipmentStatus.highAlert) ||
                                latestLog.getEquipmentStatus().equals(EquipmentStatus.alert)) {
                            //报警的状态不一致，用实时的覆盖小铃铛
                            if (!latestLog.getEquipmentStatus().equals(smallBell.getEquipmentStatus())) {
                                log.info("覆盖报警小铃铛:" + smallBell.toString());
                                SmallBell newSmallBell = new SmallBell();
                                BeanUtils.copyProperties(smallBell, newSmallBell);
                                newSmallBell.setEquipmentStatus(latestLog.getEquipmentStatus());
                                newSmallBell.setEquipmentStatusInfo(latestLog.getEquipmentStatusInfo());
                                Map<String, String> newMap = new HashMap();
                                newMap.put(equipment.getIMEI(), JSONObject.toJSONString(newSmallBell));
                                redisCache.setCacheMap("alert_small_bell:" + equipment.getSpId(), newMap);
                                list2.remove(smallBell);
                                list2.add(newSmallBell);
                            }
                        } else {
                            //判断发送消息配置中有数据并且是开着的
                            if ("ON".equals(systemAlertNotice)) {
                                //当报警记录中没有已处理的删除小铃铛
                                if (!(alarmMap.containsKey(smallBell.getImei()) && !alarmMap.get(smallBell.getImei()).isEmpty())) {
                                    //状态非报警，删除小铃铛
                                    redisCacheExt.hashDel("alert_small_bell:" + spId, smallBell.getImei());
                                    list2.remove(smallBell);
                                }
                            }else {
                                //状态非报警，删除小铃铛
                                redisCacheExt.hashDel("alert_small_bell:" + spId, smallBell.getImei());
                                list2.remove(smallBell);
                            }
                        }
                    }
                }
            }

            log.info("smallBellList-list2:{}", JSONObject.toJSONString(list2));
            List<SmallBell> lowList = list2.stream().filter(a ->
                    a.getEquipmentStatus().equals(EquipmentStatus.lowAlert) || a.getEquipmentStatus().equals(EquipmentStatus.alert)
            ).collect(Collectors.toList());
            lowList.sort(Comparator.comparing(SmallBell::getNotifyTime).reversed());
            res.setLowList(lowList);
            List<SmallBell> highList = list2.stream().filter(a -> a.getEquipmentStatus().equals(EquipmentStatus.highAlert)).collect(Collectors.toList());
            highList.sort(Comparator.comparing(SmallBell::getNotifyTime));
            res.setHighList(highList);

        } catch (Exception e) {
            log.error("小铃铛列表异常", e);
        }
        return res;
    }

    @Override
    public void updateSmallBell(Dtu equipment, String notifyTime, Consumer consumer, String orderContactName, Boolean isAddSmallBell) {
        LatestLog latestLog = latestLogService.get(equipment.getIMEI(), equipment.getSpId());
        if (latestLog == null) {
            return;
        }
        log.info(latestLog.toString());
        if (latestLog.getEquipmentStatus().equals(EquipmentStatus.lowAlert) ||
                latestLog.getEquipmentStatus().equals(EquipmentStatus.highAlert) ||
                latestLog.getEquipmentStatus().equals(EquipmentStatus.alert)) {
            if (!isAddSmallBell){
                log.info("consumerId:{} imei:{}未开启监听小铃铛的功能",consumer.getId(),equipment.getIMEI());
                return;
            }
            //查询客户信息
            SmallBell smallBell = new SmallBell();
            smallBell.setCustName(consumer.getCustName());
            smallBell.setSubsCode(consumer.getSubsCode());
            smallBell.setConsumerId(consumer.getId());
            smallBell.setImei(equipment.getIMEI());
            smallBell.setNotifyTime(notifyTime);
            smallBell.setEquipmentFrom(equipment.getEquipmentFrom());
            if (StringUtil.isNotEmpty(consumer.getMemberId())) {
                smallBell.setMemberId(consumer.getMemberId());
                //如果是紫荆wifi报警器
                if (equipment.getEquipmentFrom() == Constants.ZJ_EQUIPMENT_FROM) {
                    smallBell.setPhone(consumer.getPhone());
                }
            }
            if (StringUtil.isNotEmpty(orderContactName)) {
                smallBell.setOrderContactName(orderContactName);
            }
            BeanUtils.copyProperties(latestLog, smallBell);
            Map<String, String> map = new HashMap();
            map.put(equipment.getIMEI(), JSONObject.toJSONString(smallBell));
            log.info("新增报警小铃铛:" + smallBell.toString());
            redisCache.setCacheMap("alert_small_bell:" + equipment.getSpId(), map);
            List<Organization> parentIdBySpid = organizationService.getParentIdBySpid(equipment.getSpId());
            if (!CollectionUtils.isEmpty(parentIdBySpid)) {
                for (int i = 0; i < parentIdBySpid.size(); i++) {
                    String spId = parentIdBySpid.get(i).getParentId();
                    log.info("updateSmallBell-spId:{}", "alert_small_bell:" + spId);
                    redisCache.setCacheMap("alert_small_bell:" + spId, map);
                }
            }

        } else {
            log.info("删除报警小铃铛:" + equipment.getIMEI());
            redisCacheExt.hashDel("alert_small_bell:" + equipment.getSpId(), equipment.getIMEI());
            List<Organization> parentIdBySpid = organizationService.getParentIdBySpid(equipment.getSpId());
            if (!CollectionUtils.isEmpty(parentIdBySpid)) {
                for (int i = 0; i < parentIdBySpid.size(); i++) {
                    String spId = parentIdBySpid.get(i).getParentId();
                    redisCacheExt.hashDel("alert_small_bell:" + spId, equipment.getIMEI());
                }
            }

        }

    }

    private WarningRecord initWarningRecord(BrowserNotifyResponse notify, BrowserNotifyItemRequest item, String spId) {
        WarningRecord warningRecord = new WarningRecord();
        warningRecord.setStartTime(DateUtil.parse(notify.getNotifyTime()));
        warningRecord.setEndTime(DateUtil.parse(notify.getNotifyTime()));
        warningRecord.setWarningStartTime(DateUtil.parse(notify.getNotifyTime()));
        warningRecord.setConsumerId(notify.getConsumerId().toString());
        warningRecord.setIMEI(notify.getImei());
        warningRecord.setSupIMEI(item.getDtuId());
        warningRecord.setStatus(2);
        warningRecord.setTaskIdentificationCode("0");
        warningRecord.setConcentration(notify.getXlel());
        warningRecord.setSendType(2);
        warningRecord.setContent(JSONObject.toJSONString(notify));
        warningRecord.setNotifyObjInfo(notify.getCustName() + "(" + notify.getSubsCode() + ")");
        warningRecord.setNotifyObjType(-1);
        warningRecord.setNotifyFrom(1);
        warningRecord.setSpId(spId);
        return warningRecord;
    }
}
